package edu.hawaii.wicket.page.scheduler;


  import java.io.Serializable;
  import java.util.ArrayList;
  import java.util.Arrays;
  import java.util.List;
  /**
   * Class to create the Header.
   * @author Gerolf Seitz
   * https://github.com/gseitz
   */
  public class Header implements JsonRenderable, Serializable { 
    private static final long serialVersionUID = 1L;
    /**
     * An Enum.
     */
    public enum HeaderItem { 
      /**
       * An Enum.
       */
      title, 
      /**
       * An Enum.
       */
      prev, 
      /**
       * An Enum.
       */
      next, 
      /**
       * An Enum.
       */
      prevYear, 
      /**
       * An Enum.
       */
      nextYear, 
      /**
       * An Enum.
       */
      today, 
      /**
       * An Enum.
       */
      month,
      /**
       * An Enum.
       */
      basicWeek, 
      /**
       * An Enum.
       */
      basicDay, 
      /**
       * An Enum.
       */
      agendaWeek, 
      /**
       * An Enum.
       */
      agendaDay, 
      /**
       * An Enum.
       */
      separator;
      /**
       * An Enum.
       */
    } 
   /**
    * Enum DEFAULT.
    */
    public static final Header DEFAULT = new Header().left(HeaderItem.title).right(HeaderItem.today,
        HeaderItem.separator, HeaderItem.prev, HeaderItem.next);
    /**
     * Enum EMPTY_HEader.
     */
    public static final Header EMPTY_HEADER = new Header();

    private List<HeaderItem> left = new ArrayList<HeaderItem>();
    private List<HeaderItem> center = new ArrayList<HeaderItem>();
    private List<HeaderItem> right = new ArrayList<HeaderItem>();
    /**
     * Adds to left header.
     * @param items items to add.
     * @return the new header.
     */
    public Header left(HeaderItem... items) { 
      addHeaderItem(left, items);
      return this;
    } 
    /**
     * Adds to center Header.
     * @param items items to add.
     * @return the new header.
     */
    public Header center(HeaderItem... items) { 
      addHeaderItem(center, items);
      return this;
    } 
    /**
     * Adds to the right header.
     * @param items items to add.
     * @return the new header.
     */
    public Header right(HeaderItem... items) { 
      addHeaderItem(right, items);
      return this;
    } 


    @Override
    /**
     * Returns the hash code
     */
    public int hashCode() { 
      final int prime = 31;
      int result = 1;
      result = prime * result + ((center == null) ? 0 : center.hashCode());
      result = prime * result + ((left == null) ? 0 : left.hashCode());
      result = prime * result + ((right == null) ? 0 : right.hashCode());
      return result;
    } 

    @Override
    /**
     * Overrides equals.
     */
    public boolean equals(Object obj) { 
      if (this == obj) {
        return true;
      }
      if (obj == null) { 
        return false;
      } 
      if (getClass() != obj.getClass()) { 
        return false;
      } 
      Header other = (Header)obj;
      if (center == null) { 
        if (other.center != null) { 
          return false;
        } 
      } 
      else if (!center.equals(other.center)) { 
        return false;
      } 
      if (left == null) { 
        if (other.left != null) { 
          return false;
        } 
      } 
      else if (!left.equals(other.left)) { 
        return false;
      } 
      if (right == null) { 
        if (other.right != null) {
          return false;
        } 
      } 
      else if (!right.equals(other.right)) { 
        return false;
      } 
      return true;
    } 
    /**
     * Add to header.
     * @param list a list.
     * @param items items.
     */
    private void addHeaderItem(List<HeaderItem> list, HeaderItem... items) { 
      if (items == null) { 
        return;
      } 

      if (items.length == 1) { 
        list.add(items[0]);
      } 
      else { 
        list.addAll(Arrays.asList(items));
      } 
    } 
    /**
     * Conversion to JsonString.
     * @return a JsonString.
     */
    public String asJsonString() { 
      StringBuilder sb = new StringBuilder();
      sb.append("header: {");
      sb.append("left: '");
      addHeaderAsJson(left, sb);
      sb.append("',");
      sb.append("center: '");
      addHeaderAsJson(center, sb);
      sb.append("',");
      sb.append("right: '");
      addHeaderAsJson(right, sb);
      sb.append("'}");
      return sb.toString();
    } 
    /**
     * Adds header as a JsonString.
     * @param items items.
     * @param sb string builder.
     */
    private void addHeaderAsJson(List<HeaderItem> items, StringBuilder sb) { 
      for (HeaderItem item : items) { 
        if (item.equals(HeaderItem.separator)) { 
          sb.append(" ");
        } 
        else { 
          sb.append(item.name());
          sb.append(",");
        } 
      } 
    } 
  } 

